<?php
$loader->requireOnce('ordo/InPatient.class.php');
$loader->requireOnce('ordo/EmerReception.class.php');
/**
 * Build inpatient queue for Doctor of department emergency.
 * @package module.dept_emer.includes
 * @author hdviet
 * @version $Id$
 * @copyright 2009 LHV Company
 */
class EmerDoctorQueue implements IInPatientQueue {
	/**
	 * constructor
	 */
	function __construct() {
	}
	/**
	 * (non-PHPdoc)
	 * @see local/includes/inPatientCore/IInPatientQueue#renderQueueAjax()
	 */
	public function renderQueueAjax($departmentId, $enumStatus) {
		
		$inPatientObj = Celini::newORDO("InPatient");
		switch ( $enumStatus ) {
			case EnumerationConstant::PATIENT_SUB_STATUS_EMER_EMERGENCY:
				$arrInPatient = $inPatientObj->listOfCheckInPatientItemByStatus($departmentId, $enumStatus);
				return $this->buildEmergencyQueueHtml($arrInPatient);
			case EnumerationConstant::PATIENT_SUB_STATUS_EMER_RECOVERY:
				$arrInPatient = $inPatientObj->listOfCheckInPatientItemByStatus($departmentId, $enumStatus);
				return $this->buildRecoveryQueueHtml($arrInPatient);
			default:
				break;
		}
	}
	private function getInpatientStatusClass($injureLevel) {		
		switch ($injureLevel) {
			case EnumerationConstant::EMER_REC_INJ_LEVEL_MINOR_KEY:
				return '<img src="../images/green_circle.png" title="' . L10nHelper::_('Minor') . '"/>';
			case EnumerationConstant::EMER_REC_INJ_LEVEL_MEDIUM_KEY:
                return '<img src="../images/orange_circle.png" title="' . L10nHelper::_('Medium') . '"/>';
            case EnumerationConstant::EMER_REC_INJ_LEVEL_SERIOUS_KEY:
                return '<img src="../images/red_circle.png" title="' . L10nHelper::_('Serious') . '"/>';
            case EnumerationConstant::EMER_REC_INJ_LEVEL_DEAD_KEY:
                return 'dead';	
			default:
				;
				break;
		}

	}
	/**
	 * build html for emergency queue
	 * @param $arrInPatient array of inpatient <br>
	 * array[n](in_patient_id, patient_name, ticket_id,
	 *  enum_status, patient_code, relative_name, check_in_time)
	 * @return array('body' =>[body of emergency queue], 'total_table' => [table contain show total inpatient]);
	 */
	public function buildEmergencyQueueHtml($arrInPatient) {
		$body = "";
		$count = 1;
		
		$emerReceptionObj = Celini::newORDO("EmerReception");
		
		foreach ($arrInPatient as $row) {
			$patientId = $row['patient_id'];
			
			$emerReception = $emerReceptionObj->getLatestObjectByPatientId($patientId);
		
			$img = $this->getInpatientStatusClass($emerReception->getInjuryLevel());
			
			$room = $row['room_name'] != '' ? $row['room_name']:'NA';
			$bed = $row['bed_code'] != '' ? $row['bed_code']:'NA';
			$aRow = "<tr id=\"". $row['ticket_id']."\">";
			$aRow .= "<td>$count</td>";
			$aRow .= "<td><a href=\"" .Celini::link('RenderPatientBox', 'EmerResidentPatient') . "patientId=$patientId" ."\">" . $row['patient_name'] . "</a></td>";
			$aRow .= "<td>" . $row['patient_code'] . "</td>";			
			$aRow .= "<td>" . L10nHelper::renderDateTime($row['check_in_time'], 'short', 'datetime') . "</td>";
			$aRow .= "<td>" . $row['relative_name'] . "</td>";
			$aRow .= "<td>" . '<a href="javascript:displayChangeRoomBed(\'' . $row['ticket_id'] . '\', \'' . $row['patient_id'] . '\', \'' . $row['department_id'] . '\')">' . $room . '/' .  $bed . '</a>' . "</td>";
			$aRow .= "<td> $img </td>";
			$aRow .= "<td>" . '<a href="javascript:movetoRecoveryRoom(\'' . $row['ticket_id'] . '\',\'' . $row['patient_name'] . '\')"><span class="recovery" title="' . L10nHelper::_('Move to saving-room') . '">' . L10nHelper::_('S') . '</span></a>'
							. '<a href="javascript:displaySelectCheckOutForm(\'' . $row['ticket_id'] . '\')"><span class="checkout" title="' . L10nHelper::_('Check out patient') . '">' . L10nHelper::_('C') . '</span></a>'
							. "</td>";
			$aRow .= "<td>" . '<a href="javascript:displayEmerInstantInfo(\'' . $row['ticket_id'] . '\')"><img border="0" src="../images/arrow_down_small.png"/></a>' . "</td>";
			$aRow .= "</tr>";
			$count++;
			$body .= $aRow;
		}
		$total = '<table class="grid" cellspacing="0" cellpadding="0" style="width: 100%; border: 1px solid #E2E2E2; border-top: none;">';
		$total .= '<tr class="last"><td>'.L10nHelper::_("Total: <span class='impression'>%s<span>",--$count).'</td></tr>';
		$total .= '</table>';
		if ($body == '') {
			$body = '<tr class="last"><td colspan="9">' . L10nHelper::_('There is no result') . '</td></tr>';
		}
		return array('body' => $body, 'total_table' => $total);
	}

	/**
	 * build html for recovery queue
	 * @param $arrInPatient array of inpatient <br>
	 * array[n](in_patient_id, patient_name, ticket_id,
	 *  enum_status, patient_code, relative_name, check_in_time)
	 * @return array('body' =>[body of recovery queue], 'total_table' => [table contain show total inpatient]);
	 */
	public function buildRecoveryQueueHtml($arrInPatient) {
		$body = "";
		$count = 1;
		$emerReceptionObj = Celini::newORDO("EmerReception");
        
		foreach ($arrInPatient as $row) {
			$patientId = $row['patient_id'];
			$room = $row['room_name'] != '' ? $row['room_name']:'NA';
			$bed = $row['bed_code'] != '' ? $row['bed_code']:'NA';
			$emerReception = $emerReceptionObj->getLatestObjectByPatientId($patientId);
                
            $img = $this->getInpatientStatusClass($emerReception->getInjuryLevel());            
			$aRow = "<tr id=\"". $row['ticket_id']."\">";
			$aRow .= "<td>$count</td>";
			$aRow .= "<td><a href=\"" .Celini::link('RenderPatientBox', 'EmerResidentPatient') . "patientId=$patientId" ."\">" . $row['patient_name'] . "</a></td>";
			$aRow .= "<td>" . $row['patient_code'] . "</td>";
			
			$aRow .= "<td>" . L10nHelper::renderDateTime($row['check_in_time'], 'short', 'datetime') . "</td>";
			$aRow .= "<td>" . $row['relative_name'] . "</td>";
			$aRow .= "<td>" . '<a href="javascript:displayChangeRoomBed(\'' . $row['ticket_id'] . '\', \'' . $row['patient_id'] . '\', \'' . $row['department_id'] . '\')">' . $room . '/' .  $bed . '</a>' . "</td>";
			$aRow .= "<td> $img </td>";
			$aRow .= "<td>" . '<a href="javascript:displaySelectCheckOutForm(\'' . $row['ticket_id'] . '\')"><span class="checkout" title="' . L10nHelper::_('Check out patient') . '">' . L10nHelper::_('C') . '</span></a>' . "</td>";
			$aRow .= "<td>" . '<a href="javascript:displayRecInstantInfo(\'' . $row['ticket_id'] . '\')"><img border="0" src="../images/arrow_down_small.png"/></a>' . "</td>";
			$aRow .= "</tr>";
			$count++;
			$body .= $aRow;
		}
		$total = '<table cellspacing="0" cellpadding="0" style="width: 100%;">';
		$total .= '<tr class="last"><td>'.L10nHelper::_("Total: <span class='impression'>%s<span>",--$count).'</td></tr>';
		$total .= '</table>';
		if ($body == '') {
			$body = '<tr class="last"><td colspan="9">' . L10nHelper::_('There is no result') . '</td></tr>';
		}
		return array('body' => $body, 'total_table' => $total);
	}
}
?>